﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Win.controls
{
    public partial class ucCheck : BaseModule
    {
        public ucCheck()
        {
            InitializeComponent();

            this.Load += ucCheck_Load;
            this.windowsUIButtonPanel1.ButtonClick += windowsUIButtonPanel1_ButtonClick;

            this.grd.CellDoubleClick += grd_CellDoubleClick;
            this.grd.CellFormatting += grd_CellFormatting;
            this.grd.DataError += grd_DataError;
        }

        void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void grd_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (grd.Columns[e.ColumnIndex].Name == "supply_checkstatus")
            {
                var currentstatus = grd.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (currentstatus == CheckStatus.Qualitified.ToString())
                {
                    this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(84)))), ((int)(((byte)(175)))), ((int)(((byte)(14)))));
                }
                else if (currentstatus == CheckStatus.Rejected.ToString() || currentstatus==CheckStatus.Expired.ToString())
                {
                    //质检失败 
                    //物料过期
                    this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(94)))), ((int)(((byte)(32)))));
                }
                else if (currentstatus == CheckStatus.UnFinished.ToString())
                {
                    //未检查this.grd.Rows[e.RowIndex].DefaultCellStyle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(171)))), ((int)(((byte)(220)))));
                }
           
                else
                {
                    
                }

            }


        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as SupplyInfo;
            var frm = new frmInInfoReadonly { CurrentSupply = entitye };
            frm.ShowDialog();

            this.grd.Refresh();
        }

        void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            if (e.Button.Properties.Caption == "Check")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;

                var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;

                if (entitye.HasStorage && entitye.supply_checkstatus==CheckStatus.Qualitified)
                {
                    MessageBox.Show("已经正式入库，不能检验.");
                    return;
                }

                var frm = new frmCheckInfo {entity = entitye};
                //   frm.Saved += frm_Saved;
                frm.ShowDialog();

                this.grd.Refresh();
            }
            else if (e.Button.Properties.Caption == "Query")
            {var frm = new frmSupplyQuery();
                frm.SearchCompleted += frm_SearchCompleted;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "In")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;

                var entitye = this.grd.SelectedRows[0].DataBoundItem as SupplyInfo;
                if (entitye.supply_checkstatus != CheckStatus.Qualitified)
                {
                    MessageBox.Show("当前入库单未检验完成，检验完成后方可入库.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                var frm = new frmCreateStorage();
                frm.entity = entitye;
                frm.ShowDialog();
            }
            else
            {

            }



        }
        void frm_SearchCompleted(string where)
        {
            this.LoadData(where);
        }



        void ucCheck_Load(object sender, EventArgs e)
        {
            resource_id_column.DataSource = ResourcesHelper.GetAllResources();
            resource_id_column.DisplayMember = "ResourceName";
            resource_id_column.ValueMember = "ResourceId";


            supply_create_by.DataSource = UserInfoHelper.GetUsers();
            supply_create_by.DisplayMember = "UserName";
            supply_create_by.ValueMember = "UserId";


            supply_check_by.DataSource = UserInfoHelper.GetUsers();
            supply_check_by.DisplayMember = "UserName";
            supply_check_by.ValueMember = "UserId";

            this.LoadData("1=1");
        }
        public BindingList<SupplyInfo> Source { get; set; }
        private void LoadData(string where)
        {
            this.Source = new BindingList<SupplyInfo>(SupplyInfoHelper.GetAllSupplyInfo(where));
            this.grd.DataSource = new BindingSource(this.Source, null);
        }
    }
}
